package com.example.worldcupsever.fragment;

import android.content.Intent;
import android.graphics.drawable.Icon;
import android.os.Bundle;

import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;

import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import com.example.worldcupsever.R;
import com.example.worldcupsever.ShouDetailActivity;
import com.example.worldcupsever.bean.ShouListBean;
import com.google.gson.Gson;
import com.squareup.picasso.Picasso;

import org.jetbrains.annotations.NotNull;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import okhttp3.Call;
import okhttp3.FormBody;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;
import okhttp3.Callback;

/**
 * A simple {@link Fragment} subclass.
 */
public class ShouFragment extends Fragment {
    private static final String TAG = "ShouFragment";
    private View view;
    ListView mLvShou;
    List<ShouListBean.ListBean> shouList;
    BaseAdapter adapter;
    private FragmentManager fragmentManager;


    public ShouFragment() {
        // Required empty public constructor
    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        view = inflater.inflate(R.layout.fragment_shou, container, false);
        initView();
        initData();
        mLvShou.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                Intent intent = new Intent(getActivity(), ShouDetailActivity.class);
                intent.putExtra("shou",shouList.get(position));
                startActivity(intent);
            }
        });
        return view;
    }

    private void initData() {
        OkHttpClient client = new OkHttpClient();
        FormBody body = new FormBody.Builder().build();
        final Request request = new Request.Builder()
                .url("http://192.168.1.104:8080/WorldCupApp/shou/list")
                .post(body)
                .build();

        Call call = client.newCall(request);
        call.enqueue(new Callback() {
            @Override
            public void onFailure(@NotNull Call call, @NotNull IOException e) {
                Log.e(TAG, "onFailure: ", e);
            }

            @Override
            public void onResponse(@NotNull Call call, @NotNull Response response) throws IOException {
                //获取json字符串数据
                String jsonStr = response.body().string();//response.body().toString();错误的
                Log.e(TAG, "onResponse: " + jsonStr);
                Gson gson = new Gson();
                //解析
                ShouListBean shouListBean = gson.fromJson(jsonStr, ShouListBean.class);
                //更新数据
                shouList = shouListBean.getList();
                getActivity().runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        adapter.notifyDataSetChanged();
                    }
                });

            }
        });
    }

    private void initView() {
        mLvShou = view.findViewById(R.id.lv_shou);
        shouList = new ArrayList<>();
        adapter = new MyAdapter();
        mLvShou.setAdapter(adapter);
    }



    class MyAdapter extends BaseAdapter {

        @Override
        public int getCount() {
            return shouList.size();
        }

        @Override
        public Object getItem(int position) {
            return shouList.get(position);
        }

        @Override
        public long getItemId(int position) {
            return shouList.get(position).getId();
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            if (convertView==null){
                convertView=LayoutInflater.from(getContext()).inflate(R.layout.list_item_layout,null);
                ViewHolder viewHolder=new ViewHolder();
                viewHolder.tvShouName=convertView.findViewById(R.id.tv_shou_name);
                viewHolder.ivShouImage=convertView.findViewById(R.id.iv_shou_image);
                viewHolder.tvShouLable=convertView.findViewById(R.id.tv_shou_label);
                convertView.setTag(viewHolder);

            }
            ViewHolder vh= (ViewHolder) convertView.getTag();
            vh.tvShouName.setText(shouList.get(position).getName());
            vh.tvShouLable.setText(shouList.get(position).getLabel());
            Picasso.with(getContext())
                    .load("http://192.168.1.104:8080/WorldCupApp/"+shouList.get(position).getIcon())
                    .resize(200,200)
                    .centerCrop()
                    .into(vh.ivShouImage);

            return convertView;
        }
        class ViewHolder {
            TextView tvShouName,tvShouLable;
            ImageView ivShouImage;
        }
    }
}